<template>
  <div :class="className" :id="id" :style="{height:height,width:width}" />
</template>

<script type="text/ecmascript-6">
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'funnel'
    },
    id: {
      type: String,
      default: 'funnel'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{b} <br/>{c}人',
          textStyle: {
            fontSize: 12
          }
        },
        series: [{
          name: '漏斗图',
          type: 'funnel',
          left: 0,
          top: 20,
          bottom: 20,
          width: '100%',
          min: 0,
          max: 100,
          minSize: '6%',
          maxSize: '50%',
          sort: 'descending',
          gap: 2,
          label: {
            normal: {
              show: true,
              position: 'inside',
              fontSize: 16
            },
            emphasis: {
              textStyle: {
                fontSize: 16
              }
            }
          },
          data: [
            { value: 100, name: '浏览商品详情页' },
            { value: 65, name: '立即购买' },
            { value: 40, name: '提交订单' },
            { value: 20, name: '支付订单' }
          ],
          color: ['#55acee', '#4cdfc0', '#ff9945', '#facf2a']
        }]
      })
    }
  }
}
</script>
